from src.db_utils import *

def reupload_ticket_content():
    driver = get_webdriver()
    wait = WebDriverWait(driver, 50)
    driver.switch_to.default_content()

    try:
        db = Database()
        c = db.get_connection()
        ticket_query = ("SELECT id, custom_name, custom_phone, serial_num,"
                "custom_company, custom_department,"
                "custom_default_area, ticket_title,"
                "ticket_code, question_ask, question_done,"
                "use_time, start_time, end_time, is_import "
                "is_local, ticket_status, service_level, contract_id, "
                "product_id, l2_name, ticket_area, ticket_classify,ticket_phase, "
                "column_status,is_update,will_l2,ticket_saller,is_centos, "
                "local_start_time, local_end_time, local_service_time, local_content, "
                "local_service_area_detal,local_ticket_area,ticket_quality "
                " FROM ticket WHERE (is_worktime IS NULL OR is_worktime = '否') and ticket_status <> 4")
        c.execute(ticket_query)
        ticket_sheet = c.fetchone()

        while ticket_sheet is not None:
            ticket_id = ticket_sheet[0]
            custom_name = ticket_sheet[1]
            custom_phone = ticket_sheet[2]
            serial_num = ticket_sheet[3]
            custom_company = ticket_sheet[4]
            custom_department = ticket_sheet[5]
            custom_default_area = ticket_sheet[6]
            ticket_title = ticket_sheet[7]
            ticket_code = ticket_sheet[8]
            question_ask = ticket_sheet[9]
            question_done = ticket_sheet[10]
            use_time = ticket_sheet[11]
            start_time = ticket_sheet[12]
            end_time = ticket_sheet[13]
            is_import = ticket_sheet[14]
            is_local = ticket_sheet[15]
            ticket_status = ticket_sheet[16]
            service_level = ticket_sheet[17]
            contract_id = ticket_sheet[18]
            product_id = ticket_sheet[19]
            l2_name = ticket_sheet[20]
            ticket_area = ticket_sheet[21]
            ticket_classify = ticket_sheet[22]
            ticket_phase = ticket_sheet[23]
            column_status = ticket_sheet[24]
            is_update = ticket_sheet[25]
            will_l2 = ticket_sheet[26]
            ticket_saller = ticket_sheet[27]
            is_centos = ticket_sheet[28]
            local_start_time =ticket_sheet[29]
            local_end_time=ticket_sheet[30]
            local_service_time=ticket_sheet[31]
            local_content=ticket_sheet[32]
            local_service_area_detal=ticket_sheet[33]
            local_ticket_area=ticket_sheet[34]
            ticket_quality =ticket_sheet[35]

            ticket_classify_query = f"SELECT select_1, select_2 FROM ticket_classify where id = {ticket_classify}"
            c.execute(ticket_classify_query)
            ticket_classify_sheet = c.fetchone()
            select_1 = ticket_classify_sheet[0]
            select_2 = ticket_classify_sheet[1]

            # 点击进入本人经手工单
            wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="side-menu"]/li[2]/a'))).click()
            iframe2 = wait.until(EC.visibility_of_element_located((By.XPATH,'/html/body/div[3]/div[1]/div[4]/iframe[2]')))
            driver.switch_to.frame(iframe2)
            time.sleep(2)
            wait.until(EC.element_to_be_clickable((By.XPATH,'//div[@class="worker_left_middle"]/div/ul/li[2]'))).click()
            wait.until(EC.element_to_be_clickable((By.XPATH,'//div[@class="worker_left_middle"]/div/ul/li[2]/dl[3]'))).click()

            table_data = []

            # 1.获取到table中的L1处理中,2.点击对应编码的，3.进入iframe3
            iframe3 = wait.until(EC.visibility_of_element_located((By.XPATH,'/html/body/div[3]/div[1]/div[4]/iframe[3]')))
            driver.switch_to.frame(iframe3)

            work_text=driver.find_element(By.XPATH,'//table[@filedname="field_218"]/tbody/tr/td').text
            if work_text =='表单暂无数据':
                print("没有工时")
            else:
                print("有工时")
                pass
            
            db.close_connection()
            db = Database()
            c = db.get_connection()
            c.execute(ticket_query)
            ticket_sheet = c.fetchone()

    except sqlite3.Error as e:
        db.close_connection()
        print("An error occurred:", e)
    finally:
        db.close_connection()